package com.tange.module.log.collection;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;

import com.demo.view.LoadingDialog;
import com.tange.core.backend.service.api.CoreApiUrl;
import com.tange.core.trouble.shooting.logging.RuntimeLogging;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.appcommon.android.TGAlertDialog;
import com.tg.appcommon.android.TGLog;
import com.tange.base.toolkit.TGThreadPool;

public class AppLogManager {

    private static final String TAG = "AppLogManager";

    private static volatile AppLogManager instance;

    private LoadingDialog mLoadingDialog;

    private Handler handler = new Handler(Looper.getMainLooper());

    public static AppLogManager getInstance() {
        if (instance == null) {
            synchronized (AppLogManager.class) {
                if (instance == null) {
                    instance = new AppLogManager();
                }
            }
        }

        return instance;
    }

    private AppLogManager() {}

    private void showLoading(Activity activity) {
        hideLoading();
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(activity);
            mLoadingDialog.setMsg(com.module.commonui.R.string.uploading);
            mLoadingDialog.setCancelable(false);
        }
        if (!activity.isFinishing() && !activity.isDestroyed()) {
            mLoadingDialog.show();
        }
    }

    private void hideLoading() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.cancel();
        }
        mLoadingDialog = null;
    }

    public void uploadSilence(String from) {
        TGThreadPool.execute(() -> RuntimeLogging.INSTANCE.upload(resp -> {
            TGLog.i(TAG, "[uploadSilence] from = " + from);
            TGLog.i(TAG, "[uploadSilence] resp = " + resp);
        }));

    }

    public void upload(final Activity activity) {
        TGLog.i(TAG, "[upload] show loading");
        showLoading(activity);

        RuntimeLogging.INSTANCE.upload(resp -> {
            hideLoading();
            if (resp.getSuccess()) {
                showSuccess(activity, resp.getData());
            } else {
                showFailed(activity, "Failed: " + resp.getCode() + ", " + resp.getMessage());
            }
        });
    }

    private void showSuccess(Activity activity, String msg) {
        final String extra;
        if (CoreApiUrl.TG_HOST.contains("test")) {
            extra = "<测试环境>\n\n";
        } else {
            extra = "";
        }

        activity.runOnUiThread(() -> {
            TGAlertDialog alertDialog = new TGAlertDialog(activity)
                    .builder()
                    .setCancelable(false).setTitle(ResourcesUtil.getString(R.string.uploading_ok))
                    .setMessage(extra + ResourcesUtil.getString(R.string.upload_application_log_tag) + "[" + msg + "]" +
                            "\n\n " +
                            ResourcesUtil.getString(R.string.provide_curren_page_screenshot_for_developer))
                    .setPositiveButton(ResourcesUtil.getString(R.string.sure), v1 -> {
                        activity.finish();
                    });
            alertDialog.show();
        });
    }

    private void showFailed(Activity activity, String msg) {
        activity.runOnUiThread(() -> {
            TGAlertDialog alertDialog = new TGAlertDialog(activity)
                    .builder()
                    .setCancelable(false).setTitle(ResourcesUtil.getString(R.string.uploading_failed))
                    .setMessage(msg)
                    .setPositiveButton(ResourcesUtil.getString(R.string.sure), v1 -> {
                        activity.finish();
                    });
            alertDialog.show();
        });
    }
}
